package com.example.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.io.File;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

public class MainActivity extends AppCompatActivity {
    private Button reg;//注册
    private Button logo;//登录
    private Button info;//修改
    private Button add_stu;
    private Button query_stu;
    private Button del_stu;
    private ImageView iv_img;
    private Button list_stu;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initBtns();
    }

    private void initBtns() {
        reg=findViewById(R.id.btn_reg);
        logo=findViewById(R.id.btn_logo);
        info=findViewById(R.id.btn_info);
        add_stu=findViewById(R.id.btn_add);
        query_stu=findViewById(R.id.btn_query);
        del_stu=findViewById(R.id.btn_del);
        iv_img=findViewById(R.id.iv_img);
        list_stu=findViewById(R.id.stu_list);
        list_stu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,Stu_list.class);
                startActivity(intent);

            }
        });
        //添加学生
        add_stu.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                String filePath="/mnt/shared/Image/one.jpg";
                final BmobFile headimg=new BmobFile(new File(filePath));
                //上传图片
                headimg.upload(new UploadFileListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            Log.e("Bmob","文件上传成功");
                            Student student=new Student();
                            student.setName("兰兰");
                            student.setAge(22);
                            student.setSex("女");
                            student.setScore(88);
                            student.setHeadIMG(headimg);
                            student.save(new SaveListener<String>() {
                                @Override
                                public void done(String s, BmobException e) {
                                    if (e==null){
                                        toast("添加成功"+s);
                                    }else {
                                        toast("添加失败"+e.getMessage());
                                    }
                                }
                            });
                        }else {
                            Log.e("Bmob","文件上传失败");
                        }
                    }
                });

            }
        });
                //查询学生
                query_stu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BmobQuery<Student>studentBmobQuery=new BmobQuery<>();
                        //studentBmobQuery.addWhereEqualTo("age",20);
                        studentBmobQuery.addWhereEqualTo("name","兰兰");
                        studentBmobQuery.findObjects(new FindListener<Student>() {
                            @Override
                            public void done(List<Student> list, BmobException e) {
                                if (e==null){
                                    StringBuffer stringBuffer=new StringBuffer();
                                    for (Student student:list){
                                        stringBuffer.append(student).append("\n");
                                        BmobFile headIMG=student.getHeadIMG();
                                        Log.e("Bmob","fileUrl="+headIMG.getFileUrl());
                                        Log.e("Bmob","url="+headIMG.getUrl());
                                        Glide.with(MainActivity.this).load(headIMG.getUrl()).into(iv_img);
                                    }
                                    toast(stringBuffer.toString());
                                }

                            }
                        });
                    }
                });
                //删除学生
                del_stu.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        BmobQuery<Student>studentBmobQuery=new BmobQuery<>();
                        studentBmobQuery.addWhereEqualTo("score",88);
                        studentBmobQuery.findObjects(new FindListener<Student>() {
                            @Override
                            public void done(List<Student> list, BmobException e) {
                                if (e==null){
                                    StringBuffer stringBuffer=new StringBuffer();
                                    for (Student student:list){
                                        student.delete(new UpdateListener() {
                                            @Override
                                            public void done(BmobException e) {
                                                if (e==null){
                                                    toast("删除成功");
                                                }else {
                                                    toast("删除失败"+e.getMessage());
                                                }

                                            }
                                        });

                                    }
                                }

                            }
                        });

                    }
                });

        //用户注册
        reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               //BmobuserReg();
               Myuser();
            }
        });
        //用户登录
        logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobUser bmobUser=new BmobUser();
                bmobUser.setUsername("小小");
                bmobUser.setPassword("789");
                bmobUser.login(new SaveListener<BmobUser>() {
                    @Override
                    public void done(BmobUser bmobUser, BmobException e) {
                        if (e==null){
                            toast("登录成功"+bmobUser.getUsername());
                        }else {
                            toast("登录失败"+e.getMessage());
                        }
                    }
                });
            }
        });
        //修改用户
        info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyUser myUser=BmobUser.getCurrentUser(MyUser.class);
                myUser.setAge(80);
                myUser.setNo(1);
                myUser.update(new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e==null){
                            toast("更新成功");
                        }else {
                            toast("更新失败"+e.getMessage());
                        }
                    }
                });
            }
        });
    }
    //自定义用户注册
    private void Myuser() {
        MyUser myUser=new MyUser();
        myUser.setUsername("小小");
        myUser.setPassword("789");
        myUser.setAge(18);
        myUser.setNo(2);
        myUser.signUp(new SaveListener<MyUser>() {
            @Override
            public void done(MyUser myUser, BmobException e) {
                if (e==null){
                    toast("注册成功"+myUser.getUsername());
                }else {
                    toast("注册失败"+e.getMessage());
                }
            }
        });
    }

    //用户注册
    private void BmobuserReg() {
        BmobUser bmobUser=new BmobUser();
        bmobUser.setUsername("张三");
        bmobUser.setPassword("123456");
        bmobUser.signUp(new SaveListener<BmobUser>() {
            @Override
            public void done(BmobUser bmobUser, BmobException e) {
                if (e==null){
                    toast("注册成功"+bmobUser.getUsername());
                }else {
                    toast("注册失败"+e.getMessage());
                }
            }
        });
    }

    private void toast(String s) {
        Toast.makeText(MainActivity.this,s,Toast.LENGTH_SHORT).show();
    }
}
